<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\matrix\controller;
use think\Db;
use cmf\controller\RestBaseController;
use think\db\connector\Sqlite;
use think\response\Xml;

/**
 * 微信机器人 同步群组，会员功能
 * Class WxMemberController
 * http://192.168.2.219/api/matrix/wx_member/add_group_member.html
 * @package plugins\matrix\controller
 */
class WxMemberController extends RestBaseController
{

    /**
     * 添加矩阵会员
     * http://1o90f54084.iok.la/api/matrix/wx_member/add_matrix_member.html
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add_matrix_member(){
        $username = $_POST["username"];
        $nickname = $_POST["nickname"];
        $type = $_POST["type"];
        if($type == "2"){   //更新群名称
            $matrix_charroom["talker_name"] = $nickname;
            $matrix_charroom["update_time"] = time();
            Db::name("matrix_charroom")->where(array("talker"=>$username))->update($matrix_charroom);
        }else{
            $matrix_member = Db::name("matrix_member")->where(array("username"=>$username))->find();
            if($matrix_member){
                $matrix_member["nickname"] = $nickname;
                $matrix_member["update_time"] = time();
                Db::name("matrix_member")->update($matrix_member);
            }else{
                $matrix_member["username"] = $username;
                $matrix_member["nickname"] = $nickname;
                $matrix_member["update_time"] = time();
                $matrix_member["create_time"] = time();
                Db::name("matrix_member")->insert($matrix_member);
            }
        }
        return $this->success("OK");
    }


    /**
     * 添加群组会员信息
     * http://192.168.2.219/api/matrix/wx_member/add_group_member.html
     */
    public function add_group_member(){
        $username = $_POST["username"];
        $memberlist = $_POST["memberlist"];
        $displayname = $_POST["displayname"];
        $roomdata = $_POST["roomdata"];
        $memberlist_arr = explode(';',$memberlist);
        $displayname_arr = explode('、',$displayname);
        $matrix_charroom = Db::name("matrix_charroom")->where(array("talker"=>$username))->find();
        if($matrix_charroom){
            $matrix_charroom["memberlist"] = $memberlist;
            $matrix_charroom["member_count"] = count($memberlist_arr);
            $matrix_charroom["dispay_name"] = $displayname;
            $matrix_charroom["update_time"] = time();
            $matrix_charroom = Db::name("matrix_charroom")->update($matrix_charroom);
        }else{
            $matrix_charroom["talker"] = $username;
            $matrix_charroom["memberlist"] = $memberlist;
            $matrix_charroom["member_count"] = count($memberlist_arr);
            $matrix_charroom["dispay_name"] = $displayname;
            $matrix_charroom["update_time"] = time();
            $matrix_charroom["create_time"] = time();
            $matrix_charroom = Db::name("matrix_charroom")->insert($matrix_charroom);
        }

        for ($i=0; $i< count($memberlist_arr); $i++){
            $matrix_charroom_memberlist = Db::name("matrix_charroom_memberlist")->where(array("username"=>$memberlist_arr[$i],"talker"=>$username))->find();
            if($matrix_charroom_memberlist){
                $matrix_charroom_memberlist["username"] = $memberlist_arr[$i];
                $matrix_charroom_memberlist["dispay_name"] = $displayname_arr[$i];
                $matrix_charroom_memberlist["update_time"] = time();
                Db::name("matrix_charroom_memberlist")->update($matrix_charroom_memberlist);
            }else{
                $matrix_charroom_memberlist["talker"] = $username;
                $matrix_charroom_memberlist["username"] = $memberlist_arr[$i];
                $matrix_charroom_memberlist["dispay_name"] = $displayname_arr[$i];
                $matrix_charroom_memberlist["update_time"] = time();
                $matrix_charroom_memberlist["create_time"] = time();
                Db::name("matrix_charroom_memberlist")->insert($matrix_charroom_memberlist);
            }
        }
        return $this->success("OK");
    }

    /**
     * http://192.168.2.219/api/matrix/wx_member/wxdb.html
     * 同步群消息
     */
    public function wxdb()
    {
        //var_dump($this->uuid(""));
        // 允许上传的图片后缀
        $temp = explode(".", $_FILES["wxdb"]["name"]);
        echo "文件密码:".$_POST["password"]. "<br>";
        echo $_FILES["wxdb"]["size"]. "<br>";
        $extension = end($temp);     // 获取文件后缀名

        echo "上传文件名: " . $_FILES["wxdb"]["name"] . "<br>";
        echo "文件类型: " . $_FILES["wxdb"]["type"] . "<br>";
        echo "文件大小: " . ($_FILES["wxdb"]["size"] / 1024) . " kB<br>";
        echo "文件临时存储的位置: " . $_FILES["wxdb"]["tmp_name"] . "<br>";
        echo "文件路径".ROOT_PATH;
        // 如果 upload 目录不存在该文件则将文件上传到 upload 目录下
        move_uploaded_file($_FILES["wxdb"]["tmp_name"],  ROOT_PATH."/public/upload/" . $_FILES["wxdb"]["name"]);
        echo "文件存储在: " . ROOT_PATH."/public/upload/" . $_FILES["wxdb"]["name"];
        return $this->success("OK");
    }




    function uuid($prefix = '')
    {
        $chars = md5(uniqid(mt_rand(), true));
        $uuid  = substr($chars,0,8) . '-';
        $uuid .= substr($chars,8,4) . '-';
        $uuid .= substr($chars,12,4) . '-';
        $uuid .= substr($chars,16,4) . '-';
        $uuid .= substr($chars,20,12);
        return $prefix . $uuid;
    }

}
